package com.whale;

import java.util.concurrent.Callable;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.whale.activity.BaseActivity;
import com.whale.activity.WhaleTabActivity;
import com.whale.entity.Response;
import com.whale.entity.User;
import com.whale.entity.UserProfile;
import com.whale.util.Callback;
import com.whale.util.MsgBox;
import com.whale.util.UiUtil;
import com.whale.util.ValidUtil;

public class LoginActivity extends BaseActivity {

    private EditText etEmail;
    private EditText etPassword;
    private Button   btSubmit;
    private Button   btReg;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        etEmail = (EditText) findViewById(R.id.et_email);
        etEmail.setText("sylinx_yqg@126.com");
        etPassword = (EditText) findViewById(R.id.et_password);
        etPassword.setText("123456");
        btSubmit = (Button) findViewById(R.id.bt_login);
        btReg = (Button) findViewById(R.id.bt_reg);

        btSubmit.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                String email = etEmail.getText().toString();
                String password = etPassword.getText().toString();
                if (email.equals("")) {
                    MsgBox.showFieldEmptyErr(LoginActivity.this, getString(R.string.login_email));
                } else if (!ValidUtil.checkEmail(email)) {
                    MsgBox.showEmailErr(LoginActivity.this);
                } else if (password.equals("")) {
                    MsgBox.showFieldEmptyErr(LoginActivity.this, getString(R.string.login_password));
                } else {
                    doLogin(email, password);
                }
            }
        });

        btReg.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });

    }

    protected void onResume() {
        super.onResume();
        // 自动登录
        SharedPreferences sp = getPreferences();
        String email = sp.getString("email", null);
        String password = sp.getString("password", null);
        if (email != null && password != null) {
            etEmail.setText(email);
            etPassword.setText(password);
            doLogin(email, password);
        } else {
            if (email != null) {
                etEmail.setText(email);
            }
        }
    }

    private void doLogin(final String email, final String password) {
        UiUtil.doAsync(this, "正在登录...", new Callable<Response<User>>() {

            @Override
            public Response<User> call() throws Exception {
                return getApiService().login(email, password);
            }
        }, new Callback<Response<User>>() {

            public void onCallback(Response<User> result) {
                if (result != null) {
                    if (!result.hasError) {
                        try {
                            User user = result.data;
                            UserProfile profile = new UserProfile();
                            profile.uid = user.uid;
                            profile.email = user.email;
                            profile.name = user.name;
                            profile.avatar = user.avatar;
                            SharedPreferences.Editor editor = getPreferences().edit();
                            editor.putString("email", profile.email).putString("password", user.password).putString("uid",
                                                                                                                    profile.uid).putString("name",
                                                                                                                                           profile.name).commit();
                            setUserProfile(profile);
                            Intent intent = new Intent(LoginActivity.this, WhaleTabActivity.class);
                            startActivityForResult(intent, 0);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            MsgBox.showNetworkErr(LoginActivity.this);
                        }
                    } else {
                        MsgBox.showErrMsg(LoginActivity.this, result.errorMsg);
                        Intent intent = new Intent(LoginActivity.this, WhaleTabActivity.class);
                        startActivityForResult(intent, 0);
                    }
                } else {
                    MsgBox.showNetworkErr(LoginActivity.this);

                }
            }
        });
    }

}
